package org.yankun.exam4j.core.exam.service.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.yankun.exam4j.common.daobase.DaoBase;
import org.yankun.exam4j.core.exam.service.ExamService;
import org.yankun.exam4j.entity.Exam;
import org.yankun.exam4j.entity.UserExam;

@Transactional
@Service
public class ExamServiceImpl implements ExamService {

	@Resource
	private DaoBase<Exam> dao;
	
	@Override
	public void addExam(Exam exam) {
		Assert.notNull(exam, "Exam must not be null");
		dao.getHibernateTemplate().save(exam);
	}

	@Override
	public Exam getExamById(Serializable id) {
		return dao.getHibernateTemplate().get(Exam.class, id);
	}

	@Override
	public List<Exam> getExamsByUserId(Serializable userId) {
		String hql = "from Exam where userByCreateUserId.uid = ? order by uid desc";
		
		return dao.hql(hql,new Object[]{userId});
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<UserExam> getUserExamByUserId(Serializable userId) {
		String hql = "from UserExam e where e.user.uid = ? order by e.uid desc";
		return dao.getHibernateTemplate().find(hql,userId);
	}




//	@Override
//	public int addQuestion(Question question) {
//		Assert.notNull(question, "Question must not be null");
//		dao.getHibernateTemplate().save(question);
//		return question.getUid();
//	}
//
//
//
//
//	@Override
//	public int addPaperQuestion(PaperQuestion paperQuestion) {
//		Assert.notNull(paperQuestion, "PaperQuestion must not be null");
//		dao.getHibernateTemplate().save(paperQuestion);
//		return paperQuestion.getUid();
//	}
//
//
//
//
//	@Override
//	public void addQuestions(Paper paper, List<Question> questions) {
//		Assert.notNull(questions, "Questions must not be null");
//		
//		for(int i = 0,n = questions.size(); i<n; i++){
//			Question question = questions.get(i);
//			
//			addQuestion(question);
//			
//			PaperQuestion paperQuestion = new PaperQuestion();
//			paperQuestion.setPaper(paper);
//			paperQuestion.setQuestion(question);
//			
//			addPaperQuestion(paperQuestion);
//			
//		}
//		
//	}
//
//
//
//
//
//	@SuppressWarnings("unchecked")
//	@Override
//	public List<Exam> getExamBySchoolId(Integer schoolId, Serializable userId) {
//		
//		StringBuilder hql = new StringBuilder();
//		List<Object> values = new ArrayList<Object>();
//		
//		hql.append("from Exam where createUserId = ?");
//		values.add(userId);
//		
//		if(schoolId!=null){
//			hql.append(" or schoolId = ?");
//			values.add(schoolId);
//		}
//		
//		hql.append(" order by createTime desc ");
//		
//		return dao.getHibernateTemplate().find(hql.toString(), values.toArray());
//	}
//
//
//
//
//
//	@Override
//	public Exam getExamById(Serializable examId) {
//		String hql = "from Exam where uid = ?";
//		
//		@SuppressWarnings("unchecked")
//		List<Exam> examList = dao.getHibernateTemplate().find(hql, new Object[]{examId});
//		
//		if(examList != null && !examList.isEmpty()){
//			return examList.get(0);
//		}
//		
//		return null;
//	}
//
//
//
//
//	@SuppressWarnings("unchecked")
//	@Override
//	public List<UserExam> getUserRecentExamByUser(User user) {
//		StringBuilder hql = new StringBuilder();
//		List<Object> values = new ArrayList<Object>();
//		
//		hql.append(" from UserExam e");
//		hql.append(" where e.user.uid = ? ");
//		
//		values.add(user.getUid());
//		
//		return dao.getHibernateTemplate().find(hql.toString(), values.toArray());
//		
//	}
//
//
//
//
//
////	@Override
////	public void addFullExam(Exam exam, List<Question> questions) {
////		int examId = addExam(exam);
////		
////		List<Integer> paperIds = null;
////		
////		Set<Paper> papers = exam.getPapers();
////		if(!papers.isEmpty()){
////			paperIds = new LinkedList<Integer>();
////		}
////		
////		Iterator<Paper> it = papers.iterator();
////		while(it.hasNext()){ 
////			Paper paper = it.next(); 
////			int paperId = addPaper(paper);
////			
////			for(int i=0,n=questions.size();i<n;i++){
////				Question question = questions.get(i);
////			}
////		}
////		
////		
////	}
	

}
